package com.market.service.wx.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.market.bean.MarketBrand;
import com.market.bean.MarketBrandExample;
import com.market.bean.common.BasePageInfo;
import com.market.bean.common.CommonData;
import com.market.mapper.MarketBrandMapper;
import com.market.service.wx.WxBrandService;
import com.market.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author wh194@foxmail.com
 * @since 2022/12/31 20:33
 */
@Transactional
@Service
public class WxBrandServiceImpl implements WxBrandService {
    @Autowired
    MarketBrandMapper marketBrandMapper;

    /**
     * 微信小程序品牌列表
     * @param info
     * @param id
     * @param name
     * @return
     */
    @Override
    public CommonData<MarketBrand> list(BasePageInfo info, Integer id, String name) {
        PageHelper.startPage(info.getPage(), info.getLimit());

        MarketBrandExample example = new MarketBrandExample();
        if (info.getSort() != null) {
            example.setOrderByClause(info.getSort() + " " + info.getOrder());
        }

        MarketBrandExample.Criteria criteria = example.createCriteria();
        if (id != null) {
            criteria.andIdEqualTo(id);
        }

        if (!StringUtil.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }

        criteria.andDeletedEqualTo(false);

        List<MarketBrand> list = marketBrandMapper.selectByExample(example);
        PageInfo<MarketBrand> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }


    /**
     * 微信小程序品牌详情
     * @param id
     * @return
     */
    @Override
    public MarketBrand detail(Integer id) {
        MarketBrand marketBrand = marketBrandMapper.selectByPrimaryKey(id);

        return marketBrand;
    }

}